/**
 */
package com.dingpeikeji.quickdeer.core.modules.line.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dingpeikeji.quickdeer.core.common.config.Global;
import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.utils.StringUtils;
import com.dingpeikeji.quickdeer.core.common.web.BaseController;
import com.dingpeikeji.quickdeer.core.modules.driver.entity.TDrivers;
import com.dingpeikeji.quickdeer.core.modules.line.entity.TLine;
import com.dingpeikeji.quickdeer.core.modules.line.service.TLineService;
import com.dingpeikeji.quickdeer.core.modules.site.entity.TSitedtl;
import com.dingpeikeji.quickdeer.core.modules.site.service.TSitedtlService;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.Office;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TCargotype;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.TCargotypeService;
import com.dingpeikeji.quickdeer.modules.utils.ValidateTool;


/**
 * 线路定义Controller
 * @author Zhaoy
 * @version 2016-05-12
 */
@Controller
@RequestMapping(value = "${adminPath}/line/tLine")
public class TLineController extends BaseController {

	@Autowired
	private TLineService tLineService;
	
	@Autowired
	private TSitedtlService tSitedtlService;
	
	@ModelAttribute
	public TLine get(@RequestParam(required=false) String lineid) {
		
		if (StringUtils.isNotBlank(lineid)){
			return tLineService.get(lineid);
		}else{
			return new TLine();
		}
	}
	
	@RequiresPermissions("line:tLine:view")
	@RequestMapping(value = {"list", ""})
	public String list(TLine tLine, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TLine> page = tLineService.findPage(new Page<TLine>(request, response), tLine); 

		model.addAttribute("page", page);
		return "modules/line/tLineList";
	}

	@RequiresPermissions("line:tLine:view")
	@RequestMapping(value = "form")
	public String form(TLine tLine, Model model) {
		// 线路状态默认为有效
		if(ValidateTool.isEmptyOrNull(tLine.getLinestatus())) {
			tLine.setLinestatus(TLine.LINESTATUS_VALID);
		}
		model.addAttribute("tLine", tLine);
		TSitedtl site = new TSitedtl();
		model.addAttribute("siteList", tSitedtlService.findList(site));
		return "modules/line/tLineForm";
	}

	@RequiresPermissions("line:tLine:edit")
	@RequestMapping(value = "save")
	public String save(TLine tLine, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tLine)){
			return form(tLine, model);
		}
		//通过出发地编码设置出发点城市
		tLineService.save(tLine);
		
		addMessage(redirectAttributes, "保存线路定义成功");
		return "redirect:"+Global.getAdminPath()+"/line/tLine/?repage";
	}
	
	@RequiresPermissions("line:tLine:edit")
	@RequestMapping(value = "delete")
	public String delete(TLine tLine, RedirectAttributes redirectAttributes) {
		tLineService.delete(tLine);
		addMessage(redirectAttributes, "删除线路定义成功");
		return "redirect:"+Global.getAdminPath()+"/line/tLine/?repage";
	}

	@ResponseBody
	@RequiresPermissions("line:tLine:edit")
	@RequestMapping(value = "checklinename")
	public String checkLineName(String oldlinename, String linename) {
		if (linename !=null && linename.equals(oldlinename)) {
			return "true";
		} else if (linename !=null && tLineService.getLineByName(linename) == null) {
			return "true";
		}
		return "false";
	}

	@RequestMapping(value = "ajaxList")
	@ResponseBody
	public List<TSitedtl> ajaxList(HttpServletRequest request, HttpServletResponse response, Model model) {
		//根据站点类型查询站点列表
		String spottypes = request.getParameter("spottypes");
		TSitedtl sitedtl = new TSitedtl();
		sitedtl.setSpottypes(spottypes.split(","));
		ArrayList<TSitedtl> arrayList = (ArrayList<TSitedtl>) tSitedtlService.findList(sitedtl);
		ArrayList<TSitedtl> list = new ArrayList<TSitedtl>();
		for (int i = 0; i < arrayList.size(); i++) {
			list.add(arrayList.get(i));
		}
		return list;

	}
}
